package com.xindaoapp.happypet.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.MessageCenterAdapter;
import com.xindaoapp.happypet.bean.MessageCenterInfo;
import com.xindaoapp.happypet.fragments.MessageFragment.AudioButtonJpush;
import com.xindaoapp.happypet.ui.ChangeCenterActivity;
import com.xindaoapp.happypet.ui.MyRecordActivity;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 我的尾巴
 * 
 * @author Administrator
 * 
 */
public class IntegralFragment extends BaseFragment {
    private PullToRefreshListView mPullToRefreshListView;

    private MessageCenterAdapter mAdapter;
    private AudioButtonJpush mAudioButtonJpush;
    private View mNoData;
    private Context mContext;
    private boolean isCheckOn;

    private boolean key_bundle;

    private WeiBaCountChangedReceiver mWeiBaCountChangedReceiver;

    // pic的数据
    public int[] pics = new int[] {R.drawable.jifen_qiandao2, R.drawable.share_new,
            R.drawable.jifen_jinghua};
    // 标题


    // 金币数
    public int[] jifens = new int[] {10, 5, 10};



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mContext = getActivity();
        mWeiBaCountChangedReceiver = new WeiBaCountChangedReceiver();
        getActivity().registerReceiver(mWeiBaCountChangedReceiver,
                new IntentFilter(Constants.ACTION_WEIBA_COUNT_CHANGED));
        super.onActivityCreated(savedInstanceState);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_integral_center, container, false);
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        arguments = getArguments();
        if (arguments != null) {
            key_bundle = arguments.getBoolean("isPsersonal");
        }
        mNoData = view.findViewById(R.id.no_data);
        mPullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.DISABLED);
        mPullToRefreshListView.setOnRefreshListener(mOnRefreshListener);
        mPullToRefreshListView.getRefreshableView().setOnItemClickListener(mOnItemClickListener);
    }

    private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MessageCenterInfo info = (MessageCenterInfo) parent.getAdapter().getItem(position);
            // handleOnClick(info, ((ViewHolder) view.getTag()).isNew);
        }
    };

    /*
     * private void handleOnClick(MessageCenterInfo info, View isNew) { if
     * (info.type.equals(MESSAGETYPE.APP_PM.type)) { Intent i = new Intent(getActivity(),
     * PrivateLetterActivity.class); i.putExtra(Constants.KEY_TOUID, info.authorid);
     * i.putExtra(Constants.KEY_USERNAME, info.author); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     * getActivity().startActivity(i); } else if (info.type.equals(MESSAGETYPE.POST.type)) { Intent
     * i = new Intent(getActivity(), PostDetailActivity.class); i.putExtra("postID", info.tid);
     * i.putExtra("pageNum", info.curpage); i.putExtra("floor", info.curposition);
     * i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getActivity().startActivity(i); } else if
     * (info.type.equals(MESSAGETYPE.SYSTEM.type)) { Intent intent = new Intent(getActivity(),
     * MainActivity.class); intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
     * SystemInformFragment.class.getCanonicalName()); Bundle bundle = new Bundle();
     * intent.putExtra(Constants.KEY_BUNDLE, bundle); getActivity().startActivity(intent); } if
     * (info.isnew == 1) { info.isnew = 0; isNew.setVisibility(View.GONE);
     * HappyPetApplication.get().getMoccaApi().noticeClick(info.id, new IRequest<Boolean>() {
     * 
     * @Override public void request(Boolean t) { if (t) {
     * Constants.NoticeLabel.sHasNewMessageNotice = true; } else {
     * Constants.NoticeLabel.sHasNewMessageNotice = false; } Intent notice = new
     * Intent(Constants.KEY_NOTICE_STATE_CHANGED); getActivity().sendBroadcast(notice); } }); } }
     */


    private final OnRefreshListener<ListView> mOnRefreshListener =
            new OnRefreshListener<ListView>() {

                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    onLoadData();
                }
            };

    private Bundle arguments;

    @Override
    protected void onLoadData() {
        super.onLoadData();
        List t = new ArrayList();
        // {"每日签到", "每日分享","话题加精华"};
        t.add("每日签到");
        t.add("每日分享");
        t.add("话题加精华");
        // getMoccaApi().getMyTasklist(new IRequest<List<MyTasklist>>() {
        // //
        // @Override
        // public void request(List<MyTasklist> t) {
        if (t != null) {
            // if (t.second.size() == 0) {
            // mNoData.setVisibility(View.VISIBLE);
            // } else {
            mNoData.setVisibility(View.GONE);
            // mAdapter =
            // new MessageCenterAdapter(mActivity, t.second,
            // Constants.PAGE_SIZE, R.layout.item_message_center,
            // R.layout.item_loading);

            mPullToRefreshListView.setAdapter(new MyAdapter(t));
            // }
            onDataArrived(true);
            // } else {
            // onDataArrived(false);
        } else {
            mNoData.setVisibility(View.VISIBLE);
            onDataArrived(false);
        }
        mPullToRefreshListView.onRefreshComplete();
        // }
        // });
    }



    @Override
    public void onDestroy() {
        mContext.unregisterReceiver(mWeiBaCountChangedReceiver);
        super.onDestroy();

    }

    @Override
    protected String getTitleName() {
        return "我的尾巴币";
    }

    /*
     * @Override protected int getRightDrawable() { return R.drawable.integral_center_an; }
     */


    @Override
    protected String getRightName() {
        return "记录";
    }



    // 右边点击事件
    @Override
    protected void setRightViewListener(View view) {
        super.setRightViewListener(view);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Intent intent2 = new Intent(mContext, MainActivity.class);
                // intent2.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                // MyRecordFragment.class.getCanonicalName());
                // mContext.startActivity(intent2);
                mContext.startActivity(new Intent(mContext, MyRecordActivity.class));

            }
        });
    }


    @Override
    protected boolean showBackButton() {
        return true;
    }

    class ReceiveJpush extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            onLoadData();
        }

    }


    class MyAdapter extends BaseAdapter {

        List list = null;

        public MyAdapter(List list) {
            super();
            this.list = list;
        }

        @Override
        public int getCount() {

            return list.size() + 1 + 1;
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {

            if (position == 0) {
                contentView = View.inflate(mActivity, R.layout.fragment_integral, null);
                CircleImageView vPhoto = (CircleImageView) contentView.findViewById(R.id.pic);
                TextView vDuihuan = (TextView) contentView.findViewById(R.id.duihuan);
                TextView vName = (TextView) contentView.findViewById(R.id.name);
                TextView vJinbi = (TextView) contentView.findViewById(R.id.jinbi);
                String jinbi_text = getResources().getString(R.string.weibatext);
                if (TextUtils.isEmpty(UserState.getUser().userface)) {
                    ImageLoader.getInstance().displayImage(UserState.getUser().userface, vPhoto);
                }
                if (!TextUtils.isEmpty(UserState.getUser().username)) {
                    vName.setText(UserState.getUser().username);
                }
                vJinbi.setText(String.format(jinbi_text, UserState.getUser().niuniu));

                vDuihuan.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // Intent intent = new Intent(mActivity, MainActivity.class);
                        // intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        // ChangeCenterFragment.class.getCanonicalName());
                        // mActivity.startActivity(intent);
                        if (key_bundle) {
                            Intent intent = new Intent(mActivity, ChangeCenterActivity.class);
                            startActivity(intent);
                        } else {
                            getActivity().finish();
                        }
                    }
                });

            } else if (position == list.size() + 1) {
                contentView = View.inflate(mActivity, R.layout.item_text_desc, null);
            } else {

                contentView = View.inflate(mActivity, R.layout.item_integral_center, null);
                ImageView vPic = (ImageView) contentView.findViewById(R.id.iv_pic);
                TextView vTitle = (TextView) contentView.findViewById(R.id.title);
                TextView vJifen = (TextView) contentView.findViewById(R.id.tv_jifen);

                // MyTasklist task = list.get(position - 1);
                // vPic.setImageResource(pics[position]);
                // ImageLoader.getInstance().displayImage(task.ico, vPic);
                vPic.setImageResource(pics[position - 1]);
                vTitle.setText(list.get(position - 1) + "");
                String text = getResources().getString(R.string.weibatext);
                vJifen.setText(String.format(text, jifens[position - 1]));
            }

            return contentView;
        }
    }

    class WeiBaCountChangedReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            onLoadData();
        }

    }

}
